﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DA1
{
    public partial class PhieuNhapHang : Form
    {
        public PhieuNhapHang()
        {
            InitializeComponent();
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PhieuNhapHang_Load(object sender, EventArgs e)
        {
            PhieuNhapHang_BUS bus = new PhieuNhapHang_BUS();
            cmb_KhachHang_PN.DataSource = bus.GetDanhSachKH();
            cmb_KhachHang_PN.DisplayMember = "TenKH";
            cmb_KhachHang_PN.ValueMember = "MaKH";
            //Load NL
            DGV_PhieuNhapNL.DataSource = bus.GetDanhSachNL();

        }

        private void btn_LapPhieu_PN_Click(object sender, EventArgs e)
        {
            PhieuNhapHang_BUS bus = new PhieuNhapHang_BUS();
            PhieuNhapHang_DTO dto = new PhieuNhapHang_DTO();
            try
            {
                dto.MaKH = int.Parse(cmb_KhachHang_PN.SelectedValue.ToString());
                dto.NgayNhap = DateTime.Parse(DTPK_NgayNhap.Text.ToString());
                dto.TienTra = int.Parse(txt_SoTienTra_PN.Text.ToString());
                dto.ThanhTien = int.Parse(txt_ThanhTien_PN.Text.ToString());
                dto.MaPhieuNhap= bus.proc_Ins_PhieuNhap(dto);
                txt_MaPhieuNhap.Text = dto.MaPhieuNhap.ToString();
            }
            catch
            {
                Lbl_ThongBao.Text = "Chua tao duoc thong tin chung cua Phieu Nhap";
                Lbl_ThongBao.Visible = true;
            }
            //Insert CT ve NL cua Phieu Nhap
            try
            {
                    for (int i = 0; i < DGV_PhieuNhapNL.Rows.Count; i++)
                    {
                        if (DGV_PhieuNhapNL.Rows[i].Cells["Check_NLG"].Value == null)
                            DGV_PhieuNhapNL.Rows[i].Cells["Check_NLG"].Value = "false";
                        bool CheckBox = bool.Parse(DGV_PhieuNhapNL.Rows[i].Cells["Check_NLG"].Value.ToString());
                        if (CheckBox == true)
                        {
                            dto.MaNLG = int.Parse(DGV_PhieuNhapNL.Rows[i].Cells["PNNL_MaNLG"].Value.ToString());
                            if (DGV_PhieuNhapNL.Rows[i].Cells["PNNL_SoLuong"].Value == null)
                            {
                                Lbl_ThongBao.Text = "Chua nhap So Luong";
                                Lbl_ThongBao.Visible = true;
                                bus.XoaPhieuNhapKhiNhapCTThatBai(dto.MaPhieuNhap);
                                //Khoi tao lai cac gia tri cua cac textbox
                                txt_MaPhieuNhap.Text = "";
                                txt_SoTienTra_PN.Text = "0";
                                txt_ThanhTien_PN.Text = "0";
                                txt_TienNo_PN.Text = "0";
                                return;
                            }
                            dto.SoLuong = int.Parse(DGV_PhieuNhapNL.Rows[i].Cells["PNNL_SoLuong"].Value.ToString());
                            dto.DonGia = int.Parse(DGV_PhieuNhapNL.Rows[i].Cells["PNNL_DonGia"].Value.ToString());
                            dto.ThanhTien = dto.SoLuong * dto.DonGia;
                            txt_ThanhTien_PN.Text = bus.proc_Ins_CT_PhieuNhap(dto).ToString();
                        }
                    }
                     
                    Lbl_ThongBao.Text = "Lap Phieu Nhap NL thanh cong";
                    Lbl_ThongBao.Visible = true;
            }
            catch
            {
                bus.XoaPhieuNhapKhiNhapCTThatBai(dto.MaPhieuNhap);
                Lbl_ThongBao.Text = "Chua tao duoc Phieu Nhap NL";
                Lbl_ThongBao.Visible = true;
            }
            
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            txt_MaPhieuNhap.Text = "";
            txt_SoTienTra_PN.Text = "0";
            txt_ThanhTien_PN.Text = "0";
            txt_TienNo_PN.Text = "0";
        }

        

        

      

    }
}
